package org.hit.burkun.umls.relation;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.hit.burkun.db.DBHelper;
import org.hit.burkun.entities.Entity;
import org.hit.burkun.umls.GeneralQuery;
import org.hit.burkun.umls.relation.UniformRelation.TYPE;

/**
 * 说明文件 http://ctdbase.org/help/geneDiseaseDetailHelp.jsp The disease associated
 * with the gene. Direct evidence for the association (M marker/mechanism and/or
 * T therapeutic) The chemicals on which the inferred association is based
 * (i.e., chemicals that have curated interactions with the gene and curated
 * associations with the disease). The score for the inference based on the
 * topology of the network consisting of the gene, disease, and one or more
 * chemicals used to make the inference (see Inference Score, below). link to
 * the source reference(s) for the curated and inferred associations.
 * 
 * @author burkun
 * 
 */
public class CtdRealtionAdapter {
	// SELECT distinct disease_id, gene_id FROM biosearch.ctd_genes_diseases
	// where direct_evidence is not null;
	public static void main(String[] args) {

		Collection<UniformRelation> res = getDisease2GeneRelCurated();
		UniformRelationDump.dumpRelationsWithoutPrimaryId(res, false,
				"birelation");
	}

	public static Collection<UniformRelation> getDisease2GeneRelCurated() {
		DBHelper dbh = DBHelper.getLocalBioSearch();
		dbh.connectionDB();
		Collection<String[]> rawRes = dbh
				.queryData(
						"SELECT distinct disease_id, gene_id FROM biosearch.ctd_genes_diseases where direct_evidence is not null",
						2);
		dbh.disconnection();
		HashSet<String> terms1 = new HashSet<String>();
		HashSet<String> terms2 = new HashSet<String>();
		for (String[] line : rawRes) {
			// 分割出来
			String meshID = line[0].split(":")[1];
			terms1.add(meshID);
			terms2.add(line[1]);
		}
		// not update unmpapped
		GeneralQuery gq = new GeneralQuery(DBHelper.getServerData(),
				"map_umls_ids", false);
		gq.begin();
		gq.quickMap(terms1, Entity.TYPE.MSH);
		gq.quickMap(terms2, Entity.TYPE.GENE_ID);
		gq.end();
		Map<String, Set<String>> mapRes = gq.getResult();
		Collection<UniformRelation> res = new LinkedList<UniformRelation>();
		for (String[] line : rawRes) {
			String meshID = line[0].split(":")[1];
			if (mapRes.containsKey(meshID) && mapRes.containsKey(line[1])) {
				Set<String> umls1 = mapRes.get(meshID); // disease
				Set<String> umls2 = mapRes.get(line[1]);
				boolean isContain = OntologySiddUmlsFilter.isContain(umls1);
				if (isContain) {
					String u1 = StringUtils.join(umls1, ",");
					String u2 = StringUtils.join(umls2, ",");
					Entity ent1 = new Entity(null, null, u1, meshID,
							Entity.TYPE.DISEASE);
					Entity ent2 = new Entity(null, null, u2, line[1],
							Entity.TYPE.GENE_ID);
					TYPE type = TYPE.DISEASE_CTD_GENE_CURATED;
					if (type != null) {
						UniformRelation ur = new UniformRelation(ent1, ent2,
								false, type);
						
						res.add(ur);
					}
				}
			}
		}
		return res;
	}
}
